package com.ledim.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.bean.base.BaseResultInfo;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.LogUtil;
import com.ledim.utils.ToastUtil;
import com.ledim.widget.view.LoadingBar;
import com.letv.android.young.client.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 反馈页面
 */
public class FeedbackActivity extends LeDimBaseActivity {

    private CheckBox mSuggest;
    private CheckBox mVoidCannotPlay;
    private CheckBox mScreenroomQuestion;
    private CheckBox mAppCrash;
    private EditText mQuestionDes;
    private EditText mContactMetod;
    private Button mSubmitbtn;

    private LoadingBar mLoading;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

    }

    private void initView() {
        //title设置
        TextView mTitleView = (TextView) findViewById(R.id.user_top_view_title);
        mTitleView.setVisibility(View.VISIBLE);
        mTitleView.setText("我要反馈");
        findViewById(R.id.user_top_view_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //四个按钮
        mSubmitbtn = (Button) findViewById(R.id.bt_submit);
        mSubmitbtn.setOnClickListener(this);
        mQuestionDes = (EditText) findViewById(R.id.et_question_des);
        mContactMetod = (EditText) findViewById(R.id.et_contact_method);

        mQuestionDes.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    mSubmitbtn.setBackgroundResource(R.drawable.submit_enable_bg);
                    mSubmitbtn.setEnabled(true);
                } else {
                    mSubmitbtn.setBackgroundResource(R.drawable.submit_disabel_bg);
                    mSubmitbtn.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mSuggest = (CheckBox) findViewById(R.id.cb_suggest);
        mVoidCannotPlay = (CheckBox) findViewById(R.id.cb_video_cannot_play);
        mScreenroomQuestion = (CheckBox) findViewById(R.id.cb_screenroom_question);
        mAppCrash = (CheckBox) findViewById(R.id.cb_app_crash);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_submit:
                submitFeedback();
                break;
        }
    }

    /**
     * 获取typeStr
     */
    private String getTypeStr() {
        StringBuilder typeStr = new StringBuilder();
        if (mSuggest.isChecked()) {
            typeStr.append(",1");
        }
        if (mVoidCannotPlay.isChecked()) {
            typeStr.append(",2");
        }
        if (mScreenroomQuestion.isChecked()) {
            typeStr.append(",3");
        }
        if (mAppCrash.isChecked()) {
            typeStr.append(",4");
        }
        if (typeStr.toString().startsWith(",")) {
            return typeStr.substring(1, typeStr.length());
        }
        return "";
    }

    /**
     * 提交反馈
     */
    private void submitFeedback() {

        if (mLoading == null) {
            mLoading = new LoadingBar(this);
        }
        mLoading.show();
        LedimApiServiceFactory.createService(ApiService.class)
                              .subminFeedback(getTypeStr(), mQuestionDes.getText()
                                                                        .toString(), mContactMetod.getText()
                                                                                                  .toString())
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseResultInfo>() {
                                  @Override
                                  public void onNext(BaseResultInfo baseResultInfo) {
                                      super.onNext(baseResultInfo);
                                      if (mLoading != null) {
                                          mLoading.dismiss();
                                      }
                                      if (baseResultInfo.success) {
                                          ToastUtil.toast("感谢提交，次宝已经收到了你的反馈~");
                                          finish();
                                      } else {
                                          ToastUtil.toast(baseResultInfo.message);
                                      }
                                  }

                                  @Override
                                  public void onError(Throwable e) {
                                      super.onError(e);
                                      LogUtil.e(e.getMessage());
                                      if (mLoading != null) {
                                          mLoading.dismiss();
                                      }
                                  }
                              });
    }
}
